<?php
//ajaxsearchcart1.php
//Update shopping cart/Genus Listings when click "Add Genera" button from Free Search in Compendium search page
//Written by Hongjie Qu
//Last Updated: August 2009
?>
</html>
<SCRIPT LANGUAGE="JavaScript">

function OnDivScroll()
{
	var lstCollegeNames = document.getElementById("G");
	
	if (lstCollegeNames.options.length > 63)
	{
		lstCollegeNames.size=lstCollegeNames.options.length;
	}
	else
	{
		lstCollegeNames.size=63;	
	}	
}

	//On focus of Selectbox
	function OnSelectFocus()
	{
		
		
		if (document.getElementById("divCollegeNames").scrollLeft != 0)
		{
			document.getElementById("divCollegeNames").scrollLeft = 0;
		}


		var lstCollegeNames = document.getElementById('G');
		
		if( lstCollegeNames.options.length > 63)
		{
			lstCollegeNames.focus();
			lstCollegeNames.size=63;
		}
}

</script>

<?php

$debug=0;
$dbhost = "localhost";
$dbuser = "fossilplot";
$dbpass = "fossilplot";
$dbname = "fossilplot";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
// Retrieve data from Query String
$arrGen = $_POST['arrGen']; //arrGen is the list of genera that newly selected from left box to add to the cart
//$arrCart = $_POST['arrCart']; //arrCart is the list of genera that had been added in shopping cart last time
$cartId=$_POST['cartId'];

if($debug){
echo "hello selected Gen ".$arrGen."<br>";
echo "hello Cart ".str_replace('*','+',$arrCart)."<br>";
}

//$exploded_cart = explode(',', $arrCart);
$exploded_gen = explode(',', $arrGen);
//echo "arrCart is $arrCart<br>";




if($cartId =="No Items Selected"){
	if($arrGen) 	$cartId=$arrGen;
}
else{
	if($arrGen){
		$tmpCartId=",".$cartId.","; //there is an " " in the beginning of $cartId
				
		for($i=0;$i<sizeof($exploded_gen);$i++){
			$tmp=", ".$exploded_gen[$i].",";
			//echo "tmp is $tmp<br>";
			$pos=strpos($tmpCartId,$tmp);
			if($pos===false) {$cartId =$cartId.", ".$exploded_gen[$i];}

		}
	}


}
//echo "<br><br>new cartis is $cartId";
$groupId="(".$cartId.")";






ob_flush(); flush();


if($cartId!="No Items Selected") $len=sizeof(explode(",",$cartId));
else $len=0;
echo "<div id='countdiv'>
          
         
            <font size=2  color='993333'>$len genera are selected.</font>
 </div>";


if ($cartId=="No Items Selected"){
echo "<select name=\"G[]\" multiple size=\"63\" style=\"WIDTH: 290px\">";
echo "<option>"."No Items Selected"."</option>";
echo "</select>";
}
else{//$arrGen is not null

//echo "<div id=\"select_container\" style=\"overflow-x:scroll; width:290px; overflow: -moz-scrollbars-horizontal;\" >";

//echo "<select name=\"G[]\" multiple size=\"63\">";

echo "<div id=\"divCollegeNames\" style=\"OVERFLOW: auto;WIDTH: 304px;HEIGHT: 1007px\" onscroll=\"OnDivScroll();\">";
echo "<SELECT id=\"G\" name=\"G[]\" size=\"63\" multiple=\"\" onfocus=\"OnSelectFocus();\">";



$result = mysql_query("select id, genus_name, phylum_name, class_name, order_name,fad_date,lad_date from genus where id in $groupId");
while ($row = mysql_fetch_array($result)) {

				echo "<option>".$row{'genus_name'}."; ".$row{'phylum_name'}."; ".$row{'class_name'}.";".$row{'order_name'}."; ".$row{'fad_date'}."; ".$row{'lad_date'}."; ".$row{'id'}."</option>";

}

echo "</select>";
echo "</div>";
} //end of else ($arrGen is not null)


?>


</html>